﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.bandits {
    import flash.geom.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.sim.*;
    import Classes.effects.*;
    import Modules.workers.*;
    import Display.*;
    import Display.CopsBanditsUI.*;
    import Classes.actions.*;
    import Classes.gates.*;
    import Engine.*;
    import com.adobe.utils.*;
    import com.zynga.skelly.util.*;
    import Modules.bandits.transactions.*;

    public class BanditManager implements IGameWorldStateObserver {

        protected static const BASE_ASSET:String = "mun_policestation";
        public static const NPC_BANDIT:String = "npc_bandit";
        public static const NPC_COP:String = "npc_cop";
        public static const NPC_POLICECRUISER:String = "policecruiser";
        public static const NPC_POLICEVAN:String = "swatvan";
        public static const NPC_POLICESPORTCRUISER:String = "policesportcruiser";
        public static const NPC_POLICEHELICOPTER:String = "policehelicopter";
        public static const NUM_SWAT_COPS:int = 3;
        public static const BANDIT_NUDGE:Number = 0.9;
        public static const COPTER_FLY_HEIGHT:Number = 5;
        public static const SECURE_RAND_FEATURE_NAME:String = "copsNBandits";
        public static const ACTION_PATROL:String = "cop_patrol";
        public static const ACTION_MAKE_AND_PATROL:String = "cop_make_patrol";
        public static const ACTION_NO_BUSINESSES:String = "cop_no_bus";
        public static const ACTION_SWITCH_AND_PATROL:String = "cop_switch_patrol";
        public static const ACTION_REPLACE_AND_PATROL:String = "cop_replace_patrol";

        protected static var m_scenes:Array = [];
        protected static var m_bandits:Array = [];
        protected static var m_banditsSpawned:Array = [];
        protected static var m_cops:Array = [];
        protected static var m_copter:Helicopter;
        public static var numBanditsCaptured:Number;
        public static var typesBanditsCaptured:Object;

        private var m_gate:IGate;
        private var m_gateFactory:GateFactory;
        private var m_gateUnlockLevel:int;

        public function BanditManager(_arg1:GameWorld){
            _arg1.addObserver(this);
            this.m_gateFactory = new GateFactory();
            this.m_gateFactory.register(GateType.INVENTORY, InventoryGate);
        }
        public static function get bandits():Array{
            return (m_bandits);
        }
        public static function get banditsSpawned():Array{
            return (m_banditsSpawned);
        }
        public static function getBanditDefinitions():Array{
            var _local3:XML;
            var _local4:Object;
            var _local1:XMLList = Global.gameSettings.getBanditXML();
            var _local2:Array = new Array();
            for each (_local3 in _local1) {
                _local4 = new Object();
                _local4.id = _local3.@id;
                _local2.push(new BanditData(_local4));
            };
            return (_local2);
        }
        public static function processBusinessHarvest(_arg1:Business):void{
            var _local2:BanditData = getBanditForHarvest(_arg1);
            if (_local2 == null){
                return;
            };
            addBanditWalker(_local2, _arg1);
        }
        protected static function getBanditForHarvest(_arg1:Business):BanditData{
            var _local3:Object;
            if ((((m_bandits.length <= 0)) || (!(shouldSpawnBandit(_arg1))))){
                return (null);
            };
            var _local2:BanditData;
            if (m_bandits[m_banditsSpawned.length]){
                _local3 = new Object();
                _local3.id = m_bandits[m_banditsSpawned.length];
                m_banditsSpawned.push(_local3.id);
                _local2 = new BanditData(_local3);
            };
            return (_local2);
        }
        protected static function shouldSpawnBandit(_arg1:Business):Boolean{
            if ((((BanditUtil.getPoliceStationLevel() <= 1)) || (Global.world.citySim.banditManager.areStationsLocked()))){
                return (false);
            };
            var _local2:int = Global.gameSettings.getInt("CNBbanditSpawnChance", 5);
            var _local3 = 99;
            var _local4 = (_arg1.getItemName() + " bandit spawn");
            var _local5:int = SecureRand.randPerFeature(0, _local3, SECURE_RAND_FEATURE_NAME, _local4);
            if (_local5 < _local2){
                return (true);
            };
            return (false);
        }
        public static function addBanditWalker(_arg1:BanditData, _arg2:MapResource):void{
            var pos:* = null;
            var bandit:* = null;
            var direction:* = null;
            var def:* = _arg1;
            var building:* = _arg2;
            var road:* = Global.world.citySim.roadManager.findClosestRoad(building.getPosition());
            if (road == null){
                return;
            };
            var hotspots:* = building.getHotspots();
            if (hotspots.length > 1){
                direction = building.getHotspots()[0].subtract(building.getHotspots()[1]).normalize();
                pos = building.getHotspots()[0].add(direction.scale(BanditManager.BANDIT_NUDGE));
            } else {
                pos = road.getHotspot();
            };
            bandit = Global.world.citySim.npcManager.createWalkerByNameAtPosition(BanditManager.NPC_BANDIT, pos, false);
            bandit.animation = "idle";
            bandit.actionSelection = new BanditActionSelection(bandit);
            bandit.actionQueue.addActions(new ActionAnimationEffect(bandit, EffectType.POOF), new ActionFn(bandit, function ():void{
                Sounds.play("moneybag");
            }), new ActionPause(bandit, 2), new ActionFn(bandit, Curry.curry(function (_arg1:BanditData, _arg2:NPC):void{
                var _local3:*;
                var _local4:*;
                var _local5:*;
                if (!Global.player.getSeenFlag("banditAppeared")){
                    _local3 = ZLoc.t("Dialogs", "AppearedBandit_message");
                    _local4 = "AppearedBandit";
                    _local5 = new CharacterDialog(_local3, _local4, GenericDialogView.TYPE_OK, null, null, true, Global.gameSettings.getString("CNBcatchBanditFTUEAsset"), "Okay");
                    UI.displayPopup(_local5, true, "Bandit Seen", false);
                    Global.player.setSeenFlag("banditAppeared");
                };
                bandit.animation = "static";
            }, def, bandit)));
            bandit.slidePick = new BanditSlidePick(bandit, def);
            bandit.showSlidePick();
            bandit.playActionCallback = Curry.curry(function (_arg1:NPC):void{
                (bandit.slidePick as BanditSlidePick).banditClicked();
            }, bandit);
            BanditUtil.logGameActionStats("active_building", "police", "bandit_npc_spawn", def);
        }
        public static function addPatrol(_arg1:MapResource, _arg2:String=null, _arg3:int=-1):NPC{
            var npc:* = null;
            var npcList:* = null;
            var numNPCs:* = 0;
            var startingStation:* = null;
            var roadStart:* = null;
            var roadEnd:* = null;
            var loopsound:* = null;
            var target:* = _arg1;
            var npcType = _arg2;
            var crewSlot:int = _arg3;
            var stationLevel:* = BanditUtil.getPoliceStationLevel();
            npc = null;
            var workers:* = null;
            var copdata:* = null;
            if (stationLevel > 1){
                npcList = Global.gameSettings.getPoliceStationNPCs(stationLevel);
                numNPCs = npcList.length();
                workers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
                copdata = workers.getCopData(crewSlot);
                if ((((numNPCs > 0)) || (!((npcType == null))))){
                    npcType = (npcType) ? npcType : npcList[int((Math.random() * numNPCs))].attribute("type");
                    startingStation = getBestPoliceStation();
                    switch (npcType){
                        case BanditManager.NPC_COP:
                            npc = Global.world.citySim.npcManager.createWalkerByNameAtPosition(npcType, startingStation.getHotspot(), false);
                            npc.clearStates();
                            npc.getStateMachine().addActions(new ActionAnimationEffect(npc, EffectType.POOF));
                            npc.actionSelection = new CopNPCActionSelection(npc, target);
                            break;
                        case BanditManager.NPC_POLICECRUISER:
                        case BanditManager.NPC_POLICESPORTCRUISER:
                        case BanditManager.NPC_POLICEVAN:
                            npc = new Vehicle(npcType, false);
                            roadStart = Global.world.citySim.roadManager.findClosestRoad(startingStation.getPosition());
                            if (target){
                                roadEnd = Global.world.citySim.roadManager.findClosestRoad(target.getPosition());
                            } else {
                                roadEnd = Global.world.citySim.roadManager.findRandomRoadNearEdges(roadStart);
                            };
                            if (((!(roadStart)) && (!(roadEnd)))){
                                GlobalEngine.info("BanditManager", "Darn, no road(s) available for the cop cruiser");
                                npc.cleanUp();
                                return (addPatrol(target, BanditManager.NPC_COP, crewSlot));
                            };
                            if (!Global.world.citySim.roadManager.findPath(roadStart, roadEnd, RoadManager.PATH_ROAD_ONLY)){
                                roadStart = Global.world.citySim.roadManager.findRandomRoadNearEdges(roadEnd);
                            };
                            if (!roadStart){
                                npc.cleanUp();
                                return (addPatrol(target, BanditManager.NPC_COP, crewSlot));
                            };
                            npc.setPosition(roadStart.getHotspot().x, roadStart.getHotspot().y);
                            Global.world.citySim.npcManager.addVehicle((npc as Vehicle));
                            npc.clearStates();
                            npc.getStateMachine().addActions(new ActionAnimationEffect(npc, EffectType.VEHICLE_POOF), new ActionFn(npc, function ():void{
                                Sounds.play("siren");
                                loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
                            }), new ActionNavigate(npc, roadEnd, null), new ActionFn(npc, function ():void{
                                Sounds.stop(loopsound);
                            }), new ActionFn(npc, Curry.curry(BanditManager.swapCarForCop, (npc as Vehicle), target)));
                            break;
                        default:
                            GlobalEngine.warning("BanditManager", ("Unknown npc type: " + npcType));
                    };
                } else {
                    GlobalEngine.warning("BanditManager", "Patrol requested but no npc available or specified");
                };
            } else {
                GlobalEngine.warning("BanditManager", "Patrol requested when no upgraded police stations available");
            };
            if (npc){
                m_cops.push(npc);
                npc.playActionCallback = Curry.curry(function ():void{
                });
                if (copdata != null){
                    npc.slidePick = new CopSlidePick(npc, copdata);
                    npc.highlightCallback = function (_arg1:Boolean, _arg2:Boolean, _arg3:Boolean=true):void{
                        var received:* = _arg1;
                        var stored:* = _arg2;
                        var showText:Boolean = _arg3;
                        if (((received) && (!(npc.isSlidePickShown())))){
                            npc.showSlidePick();
                            if (showText){
                                (npc.slidePick as CopSlidePick).showText();
                            } else {
                                (npc.slidePick as CopSlidePick).hideText();
                            };
                            TimerUtil.callLater(function ():void{
                                npc.hideSlidePick(0.5);
                            }, 5000);
                        };
                    };
                    npc.imageLoadedCallback = Curry.curry(function (_arg1:NPC):void{
                        _arg1.highlightCallback(true, false, false);
                        _arg1.imageLoadedCallback = null;
                    }, npc);
                };
                if (((!((crewSlot == -1))) && (workers))){
                    copdata.setCopReference(npc);
                    workers.setCopData(copdata);
                };
            };
            return (npc);
        }
        public static function swapCarForCop(_arg1:Vehicle, _arg2:MapResource):NPC{
            var _local3:int = m_cops.indexOf(_arg1);
            var _local4:Vector3 = _arg1.getPosition();
            var _local5:String = _arg1.getItem().name;
            var _local6:NPC = Global.world.citySim.npcManager.createWalkerByNameAtPosition(BanditManager.NPC_COP, _local4, false);
            _local6.clearStates();
            _local6.getStateMachine().addActions(new ActionAnimationEffect(_local6, EffectType.POOF));
            _local6.actionSelection = new CopNPCActionSelection(_local6, _arg2);
            _local6.slidePick = _arg1.slidePick;
            _local6.highlightCallback = _arg1.highlightCallback;
            if (_local6.slidePick){
                (_local6.slidePick as CopSlidePick).owner = _local6;
            };
            var _local7:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            var _local8:CopData = _local7.getNpcOwner(_arg1);
            if (_local8){
                _local7.setNpcOwner(_local8.getPosition(), _local6);
            };
            Global.world.citySim.npcManager.removeVehicle(_arg1);
            if (_local3 > -1){
                m_cops.splice(_local3, 1, _local6);
            };
            return (_local6);
        }
        public static function spawnStationHelicopter():void{
            if (!m_copter){
                m_copter = createHelicopter(false, true);
            };
        }
        protected static function createHelicopter(_arg1:Boolean, _arg2:Boolean=false):Helicopter{
            var _local4:Vector2;
            var _local5:Array;
            var _local6:int;
            var _local7:MapResource;
            var _local8:Number;
            var _local9:Number;
            var _local10:Number;
            var _local11:Number;
            var _local12:Number;
            var _local13:Number;
            if (m_copter){
                return (m_copter);
            };
            if (((!(_arg1)) && ((BanditUtil.getPoliceStationLevel() < 6)))){
                return (null);
            };
            var _local3:Helicopter = new Helicopter(BanditManager.NPC_POLICEHELICOPTER, false);
            Global.world.citySim.npcManager.addVehicle(_local3);
            if (_arg2){
                _local5 = Global.world.getObjectsByPredicate(BanditUtil.isPolice);
                _local6 = (_local5.length - 1);
                while (_local6 >= 0) {
                    if (Global.gameSettings.getPoliceStationLevel(_local5[_local6].getItem().name) < 6){
                        _local5.splice(_local6, 1);
                    };
                    _local6--;
                };
                if (_local5.length){
                    _local7 = MathUtil.randomElement(_local5);
                    _local4 = new Vector2((_local7.getPosition().x + 15), (_local7.getPosition().y - 15));
                } else {
                    GlobalEngine.warning("BanditManager", "Created helicopter with no pads available");
                };
            } else {
                _local8 = Global.stage.y;
                _local9 = (Global.stage.stageHeight / 2);
                _local10 = Global.stage.x;
                _local11 = Global.stage.stageWidth;
                _local12 = ((_local11 + _local10) + 20);
                _local13 = (_local8 + _local9);
                _local4 = IsoMath.screenPosToTilePos(_local12, _local13);
            };
            _local3.setPosition(_local4.x, _local4.y, 10);
            return (_local3);
        }
        public static function cleanUpHelicopter(_arg1:Helicopter):void{
            if (m_copter == _arg1){
                m_copter = null;
            };
        }
        public static function isActiveCopter(_arg1:Helicopter):Boolean{
            return ((_arg1 == m_copter));
        }
        public static function sendHelicopter(_arg1:Vector3, _arg2:Boolean, _arg3:Function=null):void{
            var _local4:Helicopter = createHelicopter(_arg2);
            if (_local4){
                if (!_arg2){
                    m_copter = _local4;
                };
                _local4.addScene(_arg1);
            };
        }
        public static function pullCopForArrest(_arg1:Vector3=null, _arg2:Boolean=false):NPC{
            var _local4:Number;
            var _local5:Number;
            var _local6:Number;
            var _local7:Number;
            var _local8:NPC;
            var _local9:Array;
            var _local10:CopData;
            var _local3:NPC;
            if (_arg2){
                _local3 = Global.world.citySim.npcManager.createWalkerByNameAtPosition(BanditManager.NPC_COP, _arg1, false);
                return (_local3);
            };
            if (_arg1){
                _local4 = 0;
                for each (_local8 in m_cops) {
                    if (_local8){
                        _local5 = (_local8.getPosition().x - _arg1.x);
                        _local6 = (_local8.getPosition().y - _arg1.y);
                        _local7 = ((_local5 * _local5) + (_local6 * _local6));
                        if ((((_local7 < _local4)) || ((_local3 == null)))){
                            _local3 = _local8;
                            _local4 = _local7;
                        };
                    };
                };
            } else {
                if (m_cops.length){
                    _local3 = m_cops[int((Math.random() * m_cops.length))];
                };
            };
            if ((_local3 is Vehicle)){
                _local3 = BanditManager.swapCarForCop((_local3 as Vehicle), null);
            };
            ArrayUtil.removeValueFromArray(m_cops, _local3);
            if (!_local3){
                _local3 = Global.world.citySim.npcManager.createWalkerByNameAtPosition(BanditManager.NPC_COP, _arg1, false);
                _local9 = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers).getAllCopData();
                for each (_local10 in _local9) {
                    if ((((_local10.getState() == CopData.STATE_SLEEPING)) && (((_local10.getTimestamp() + Global.gameSettings.getInt("CNBcopSleepTime")) < Math.floor((GlobalEngine.getTimer() / 1000)))))){
                        (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers).setNpcOwner(_local10.getPosition(), _local3);
                        break;
                    };
                };
            };
            if (_local3){
                _local3.clearStates();
            } else {
                GlobalEngine.warning("BanditManager", "Failed to pull cop.");
            };
            return (_local3);
        }
        public static function createArrestScene(_arg1:BanditData, _arg2:Vector3=null, _arg3:NPC=null, _arg4:Boolean=false):ArrestScene{
            var _local5:ArrestScene = new ArrestScene(_arg1, _arg2, _arg4);
            _local5.bandit = _arg3;
            _local5.setOuter(Global.world);
            _local5.attach();
            _local5.init();
            m_scenes.push(_local5);
            return (_local5);
        }
        public static function removeArrestScene(_arg1:ArrestScene):void{
            if (!ArrayUtil.arrayContainsValue(m_scenes, _arg1)){
                return;
            };
            ArrayUtil.removeValueFromArray(m_scenes, _arg1);
            _arg1.shutdown();
            _arg1.detach();
            _arg1.cleanUp();
        }
        public static function getArrestScenes():Array{
            return (m_scenes);
        }
        public static function get cops():Array{
            return (m_cops);
        }
        public static function get numPatrollingCops():int{
            var _local3:CopData;
            var _local1:int;
            var _local2:Array = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers).getAllCopData();
            for each (_local3 in _local2) {
                if (_local3.getState() == CopData.STATE_PATROLLING){
                    _local1++;
                };
            };
            return (_local1);
        }
        public static function get numWalkerCops():int{
            return (m_cops.length);
        }
        public static function get numSleepingCops():int{
            var _local3:Array;
            var _local4:CopData;
            var _local1:int;
            var _local2:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            if (_local2){
                _local3 = _local2.getAllCopData();
                for each (_local4 in _local3) {
                    if (_local4.getState() == CopData.STATE_SLEEPING){
                        _local1++;
                    };
                };
            };
            return (_local1);
        }
        public static function get numUsableCops():int{
            var _local3:Array;
            var _local4:CopData;
            var _local1:int;
            var _local2:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            if (_local2){
                _local3 = _local2.getAllCopData();
                for each (_local4 in _local3) {
                    if ((((_local4.getState() == CopData.STATE_PATROLLING)) || ((((_local4.getState() == CopData.STATE_SLEEPING)) && (((_local4.getTimestamp() + Global.gameSettings.getInt("CNBcopSleepTime")) <= Math.floor((GlobalEngine.getTimer() / 1000)))))))){
                        _local1++;
                    };
                };
            };
            return (_local1);
        }
        public static function getBestPoliceStation():Municipal{
            var _local4:Array;
            var _local5:Municipal;
            var _local6:RoadGraph;
            var _local7:Road;
            var _local8:Number;
            var _local1:Municipal;
            var _local2:Number = 0;
            var _local3:int = BanditUtil.getPoliceStationLevel();
            if (_local3 > 0){
                _local4 = Global.world.getObjectsByNames([Global.gameSettings.getPoliceStationNameForLevel(_local3)]);
                for each (_local5 in _local4) {
                    _local6 = Global.world.citySim.roadManager.getConnectedRoadGraph(_local5);
                    _local7 = Global.world.citySim.roadManager.findClosestRoad(_local5.getPosition());
                    if (_local6){
                        return (_local5);
                    };
                    if (_local7){
                        _local8 = MathUtil.distance(new Point(_local7.getPosition().x, _local7.getPosition().y), new Point(_local5.getPosition().x, _local5.getPosition().y));
                        if ((((_local8 < _local2)) || ((_local1 == null)))){
                            _local2 = _local8;
                            _local1 = _local5;
                        };
                    };
                };
                if (!_local1){
                    _local1 = _local4[0];
                };
            };
            return (_local1);
        }
        public static function getActivatedUpgrades():Dictionary{
            var station:* = null;
            var upgrade:* = null;
            var name:* = null;
            var results:* = new Dictionary();
            var allStations:* = Global.world.getObjectsByPredicate(BanditUtil.isPolice);
            var allStationNames:* = allStations.map(function (_arg1:Municipal, ... _args):String{
                return (_arg1.getItem().name);
            });
            var upgrades:* = UpgradeDefinition.getUpgradeChainNames(BASE_ASSET);
            upgrades.unshift(BASE_ASSET);
            for each (upgrade in upgrades) {
                if (ArrayUtil.arrayContainsValue(allStationNames, upgrade)){
                    break;
                };
                results[upgrade] = true;
            };
            for each (name in allStationNames) {
                results[name] = true;
            };
            return (results);
        }
        public static function upgradeAllStations(_arg1:int=0, _arg2:String=""):Boolean{
            var _local4:Municipal;
            var _local5:Array;
            var _local6:Array;
            if ((((_arg1 == 0)) && (!(canUpgradeAllStations())))){
                return (false);
            };
            var _local3:Array = Global.world.getObjectsByPredicate(BanditUtil.isPolice);
            if (_arg1 == 0){
                for each (_local4 in _local3) {
                    _local4.upgradeBuildingIfPossible();
                };
            } else {
                _local5 = new Array();
                _local6 = new Array();
                for each (_local4 in _local3) {
                    _local5.push(_local4);
                    if (_arg2 != ""){
                        _local6.push(_arg2);
                    } else {
                        _local6.push(_local4.getItem().upgrade.newItemName);
                    };
                };
                GameTransactionManager.addTransaction(new TBuyUpgradeBuildings(_local5, _local6, _arg1));
            };
            Global.world.citySim.banditManager.setStationUnlockLevel(BanditUtil.getPoliceStationLevel());
            return (true);
        }
        public static function upgradeCatchUp(_arg1:Municipal):void{
            var _local2:int = Math.max(BanditUtil.getPoliceStationLevel(), Global.world.citySim.banditManager.getStationUnlockLevel());
            var _local3:int = BanditUtil.getPoliceStationInstanceLevel(_arg1);
            if ((((_local2 > 0)) && ((_local3 < _local2)))){
                GameTransactionManager.addTransaction(new TUpgradeCatchUp(_arg1));
            };
        }
        public static function canUpgradeAllStations():Boolean{
            var _local2:Municipal;
            var _local1:Array = Global.world.getObjectsByPredicate(BanditUtil.isPolice);
            for each (_local2 in _local1) {
                if (!_local2.isUpgradePossible()){
                    return (false);
                };
            };
            return (true);
        }
        public static function getCopSleepTime():Number{
            return (Global.gameSettings.getNumber("CNBcopSleepTime", 0));
        }
        public static function spawnExistingCops(_arg1:Boolean=false):void{
            var _local3:Array;
            var _local4:CopData;
            if (_arg1){
                m_cops = [];
                m_copter = null;
            };
            var _local2:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            if (_local2){
                _local3 = _local2.getAllCopData();
                for each (_local4 in _local3) {
                    if (_arg1){
                        _local4.setCopReference(null);
                    };
                    if (numPatrollingCops > numWalkerCops){
                        if ((((((_local4.getState() == CopData.STATE_PATROLLING)) || ((((_local4.getState() == CopData.STATE_SLEEPING)) && (((_local4.getTimestamp() + Global.gameSettings.getInt("CNBcopSleepTime")) < Math.floor((GlobalEngine.getTimer() / 1000)))))))) && ((_local4.getCopReference() == null)))){
                            BanditManager.addPatrol(null, null, _local4.m_position);
                        };
                    } else {
                        break;
                    };
                };
            };
            if (BanditUtil.getPoliceStationLevel() >= 6){
                BanditManager.spawnStationHelicopter();
            };
        }
        public static function spawnAmbientCopToDonutShop():void{
        }
        public static function getNumBanditsUntilUpgrade():int{
            var _local2:UpgradeDefinition;
            var _local3:int;
            var _local1:int = BanditUtil.getPoliceStationLevel();
            if (_local1 > 0){
                _local2 = UpgradeDefinition.getNextUpgrade(Global.gameSettings.getPoliceStationNameForLevel(_local1));
                if (_local2){
                    _local3 = int(_local2.requirements.getRequirementValue(Requirements.REQUIREMENT_SPECIAL_BANDITS));
                    return (Math.max(0, (_local3 - numBanditsCaptured)));
                };
                return (-1);
            };
            return (-1);
        }
        public static function findDonutHungryCop():int{
            var _local4:CopData;
            var _local1 = -1;
            var _local2:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            var _local3:Array = _local2.getAllCopData();
            for each (_local4 in _local3) {
                if (_local4.getState() == CopData.STATE_SLEEPING){
                    _local1 = _local4.getPosition();
                    break;
                };
            };
            return (_local1);
        }
        public static function giveDonutAndWake(_arg1:int):String{
            var _local2:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            var _local3:CopData = _local2.getCopData(_arg1);
            _local3.setState(CopData.STATE_PATROLLING);
            _local3.setTimestamp(Math.floor((GlobalEngine.getTimer() / 1000)));
            _local2.setCopData(_local3);
            return (_local3.getState());
        }
        public static function addDonutToCop(_arg1:String=""):void{
            var _local2:int = BanditManager.findDonutHungryCopById(_arg1);
            if (_local2 != -1){
                BanditManager.giveDonutAndWake(_local2);
            } else {
                _local2 = BanditManager.findDonutHungryCop();
                if (_local2 != -1){
                    BanditManager.giveDonutAndWake(_local2);
                };
            };
        }
        public static function findDonutHungryCopById(_arg1:String=""):int{
            var _local5:CopData;
            var _local2 = -1;
            var _local3:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            var _local4:Array = _local3.getAllCopData();
            for each (_local5 in _local4) {
                if ((((_local5.getState() == CopData.STATE_SLEEPING)) && ((_local5.m_ZID == _arg1)))){
                    _local2 = _local5.getPosition();
                    break;
                };
            };
            return (_local2);
        }

        public function initialize():void{
        }
        public function cleanUp():void{
        }
        public function onGameLoaded(_arg1:Object):void{
            var _local2:int = BanditUtil.getPoliceStationLevel();
            if (_local2 >= 2){
                this.preloadAssets(_local2);
            };
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        public function areStationsLocked():Boolean{
            return (false);
        }
        public function getStationUnlockLevel():int{
            return (this.m_gateUnlockLevel);
        }
        public function setStationUnlockLevel(_arg1:int):void{
            this.m_gateUnlockLevel = _arg1;
        }
        private function preloadAssets(_arg1:int=0):void{
            var _local9:Item;
            var _local10:Item;
            var _local11:Item;
            var _local12:Item;
            var _local13:Item;
            var _local14:Item;
            if (_arg1 < 2){
                return;
            };
            var _local2:Array = [Item.DIRECTION_NW, Item.DIRECTION_NE, Item.DIRECTION_SE, Item.DIRECTION_SW];
            var _local3:Array = ["arrest_1", "arrest_2", "arrest_3"];
            var _local4:Array = ["arrest_1", "arrest_2", "arrest_3"];
            var _local5:Array = [Item.DIRECTION_N, Item.DIRECTION_E, Item.DIRECTION_S, Item.DIRECTION_W, Item.DIRECTION_NW, Item.DIRECTION_NNW, Item.DIRECTION_NWW, Item.DIRECTION_NE, Item.DIRECTION_NNE, Item.DIRECTION_NEE, Item.DIRECTION_SE, Item.DIRECTION_SSE, Item.DIRECTION_SEE, Item.DIRECTION_SW, Item.DIRECTION_SSW, Item.DIRECTION_SWW];
            var _local6:Array = ["static"];
            var _local7:int;
            var _local8:int;
            _local9 = Global.gameSettings.getItemByName(NPC_COP);
            _local10 = Global.gameSettings.getItemByName(NPC_BANDIT);
            _local7 = 0;
            while (_local7 < _local2.length) {
                _local8 = 0;
                while (_local8 < _local3.length) {
                    _local9.getCachedImage(_local3[_local8], null, _local2[_local7]);
                    _local8++;
                };
                _local8 = 0;
                while (_local8 < _local4.length) {
                    _local10.getCachedImage(_local4[_local8], null, _local2[_local7]);
                    _local8++;
                };
                _local7++;
            };
            if (_arg1 < 3){
                return;
            };
            _local11 = Global.gameSettings.getItemByName(NPC_POLICECRUISER);
            _local12 = ((_arg1)>3) ? Global.gameSettings.getItemByName(NPC_POLICEVAN) : null;
            _local13 = ((_arg1)>4) ? Global.gameSettings.getItemByName(NPC_POLICESPORTCRUISER) : null;
            _local14 = ((_arg1)>5) ? Global.gameSettings.getItemByName(NPC_POLICEHELICOPTER) : null;
            _local7 = 0;
            while (_local7 < _local5.length) {
                _local8 = 0;
                while (_local8 < _local6.length) {
                    if (_local11){
                        _local11.getCachedImage(_local6[_local8], null, _local5[_local7]);
                    };
                    if (_local12){
                        _local12.getCachedImage(_local6[_local8], null, _local5[_local7]);
                    };
                    _local8++;
                };
                _local7++;
            };
        }
        public function showPoliceStationGate(_arg1:Municipal):void{
            if (!BanditUtil.isPolice(_arg1)){
                throw (new Error(("Invalid police station for gating: " + _arg1.getItem().name)));
            };
            if (this.m_gate == null){
                this.m_gate = this.m_gateFactory.loadGateFromXML(_arg1.getItem(), _arg1, "upgrade", Curry.curry(this.onUpgradeGateSuccess, _arg1));
            };
            if (this.m_gate == null){
                throw (new Error(("Failed to initialize gate for item " + _arg1.getItem().name)));
            };
            this.m_gate.displayGate();
        }
        public function onUpgradeGateSuccess(_arg1:Municipal):void{
            var _local2:String;
            var _local3:String;
            var _local4:CharacterDialog;
            GameTransactionManager.addTransaction(new TUnlockPoliceGate(_arg1));
            BanditUtil.logGameActionStats("municipal", "unlock_gate", _arg1.getItem().name);
            this.m_gateUnlockLevel = BanditUtil.getPoliceStationLevel();
            BanditUtil.refreshPoliceStationAppearance();
            if (this.m_gateUnlockLevel == 2){
                this.preloadAssets(this.m_gateUnlockLevel);
                BanditManager.spawnExistingCops();
                _local2 = ZLoc.t("Dialogs", "FinishedPoliceStation_message");
                _local3 = "FinishedPoliceStation";
                _local4 = new CharacterDialog(_local2, _local3, GenericDialogView.TYPE_OK, null, null, true, Global.gameSettings.getString("CNBcatchBanditFTUEAsset"), "Okay");
                UI.displayPopup(_local4, true, "Finished Police Station", false);
            };
        }
        public function parseServerData(_arg1:Object):void{
            m_bandits = _arg1.bandits;
            numBanditsCaptured = _arg1.numBanditsCaptured;
            this.m_gateUnlockLevel = ((_arg1.gateUnlockLevel) || (1));
            if (!_arg1.typesBanditsCaptured){
                typesBanditsCaptured = {};
            } else {
                typesBanditsCaptured = _arg1.typesBanditsCaptured;
            };
        }

    }
}//package Modules.bandits 
